﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmRestore : FrmDefault
    {
        private List<PhoneBook> result = new List<PhoneBook>();
        public FrmRestore()
        {
            InitializeComponent();
        }

        private void LoadFilesBkp()
        {
            DirectoryInfo Dir = new DirectoryInfo(Application.UserAppDataPath);
            FileInfo[] Files = Dir.GetFiles("PhoneManager.Backup." + SharedLib.ModemConfig.Serial + ".*.dat");
            LstFiles.Items.Clear();
            foreach (FileInfo item in Files)
            {
                LstFiles.Items.Add(item.Name);
            }
            LstFiles.SelectedIndex = -1;
        }
        private void ShowInfBakup(List<PhoneBook> Bkp)
        {
            int QtdSIM = 0;
            int QtdPhone = 0;
            LstNames.Items.Clear();
            foreach (PhoneBook item in Bkp)
            {
                LstNames.Items.Add(item.Name + "  (" + item.Number + ")");
                if (item.SIMIndex != "")
                {
                    QtdSIM++;
                }
                if (item.PhoneIndex != "")
                {
                    QtdPhone++;
                }
            }
            LblQtdP.Text = QtdPhone.ToString();
            LblQtdS.Text = QtdSIM.ToString();
        }
        private void FrmRestore_Load(object sender, EventArgs e)
        {
            LoadFilesBkp();
        }

        private void LstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            LblQtdP.Text = "0";
            LblQtdS.Text = "0";
            LstNames.Items.Clear();
            result = new List<PhoneBook>(); 
            if (LstFiles.SelectedIndex == -1)
            {
                BntRestore.Enabled = false;
                return;
            }
            BntRestore.Enabled = true;
            using (FileStream fs = new FileStream(Application.UserAppDataPath + "\\" + LstFiles.SelectedItem.ToString(), FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    result = (List<PhoneBook>)formatter.Deserialize(fs);
                }
                catch (Exception ex)
                {
                    SharedLib.MsgErro(ex.Message);
                }
            }
            ShowInfBakup(result);
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BntRestore_Click(object sender, EventArgs e)
        {
            bool show = false;
            int qtdok = 0;
            int qtdnok = 0;
            foreach (PhoneBook item in result)
            {
                item.SIMIndex = "";
                item.PhoneIndex = "";
                PhoneBook pb = SharedLib.FindPhoneByNumber(item.Number, false);
                if (pb == null)
                {
                    pb = SharedLib.FindPhoneByNumber(item.Number, true);
                }
                if (pb == null)
                {
                    qtdok++;
                    item.RestoreIndex = qtdok;
                    SharedLib.PhonesBook.Add(item);
                    show = true;
                }
                else
                {
                    qtdnok++;
                }
            }
            SharedLib.PhonesBook.Sort(new OrderPhoneBook());
            SharedLib.ShowPhoneBook = show;
            if (qtdnok != 0)
            {
                SharedLib.MsgAlert(qtdnok.ToString() + " " + ResourcesText.NoRestoreNumbers );   
            }
            this.Close();
        }
    }
}
